package doctor

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"net/http"
	__ "srv/proto"
)

type Add struct {
	Name        string  `form:"name" json:"name" xml:"name"`
	Image       string  `form:"image" json:"image" xml:"image"`
	CollegeArea string  `form:"collegeArea" json:"collegeArea" xml:"collegeArea"`
	ClassArea   string  `form:"classArea" json:"classArea" xml:"classArea"`
	Level       string  `form:"level" json:"level" xml:"level"`
	Price       float64 `form:"price" json:"price" xml:"price"`
	Prices      float64 `form:"prices" json:"prices" xml:"prices"`
	Intro       string  `form:"intro" json:"intro" xml:"intro"`
}

func AddDoctor(c *gin.Context) {
	var json Add
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	// 1.连接
	conn, err := grpc.Dial("127.0.0.1:8888", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	defer conn.Close()
	// 2. 实例化gRPC客户端
	client := __.NewDoctorServiceClient(conn)
	// 3.组装请求参数
	req := new(__.DoctorAddRequest)
	req.Name = json.Name
	req.Image = json.Image
	req.CollegeArea = json.CollegeArea
	req.ClassArea = json.ClassArea
	req.Level = json.Level
	req.Price = float32(json.Price)
	req.Prices = float32(json.Prices)
	req.Intro = json.Intro
	// 4. 调用接口
	response, err := client.DoctorAdd(context.Background(), req)
	if err != nil {
		fmt.Println("响应异常  %s\n", err)
	}
	fmt.Printf("响应结果： %d", response.Id)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "注册成功",
		"data":    response.Id,
	})
}
